//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using Content = CommunityServer.Components.Content;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for Contents.
	/// </summary>
	public class Contents
	{
		private Contents()
		{
		}

        #region Data
        public static IList GetAllContent()
        {
            CommonDataProvider dp = CommonDataProvider.Instance();
            return dp.GetContentList();
        }

        public static Content GetContent(int id, bool cached)
        {
            // try and retreive from cache
            Content content = null;
            
            if(cached)
            {
                content = CSCache.Get(CacheKey(id.ToString())) as Content;
            }

            if (content == null)
            {
                CommonDataProvider dp = CommonDataProvider.Instance();
                content = dp.GetContent(id, null);
                
                if(cached && content != null)
                {
                    CSCache.Insert(CacheKey(content.Name), content, CSCache.MinuteFactor);
                    CSCache.Insert(CacheKey(content.ID.ToString()), content, CSCache.MinuteFactor);
                }
            }

            return content;
        }

        public static Content GetContent(string name)
        {
            // try and retreive from cache
            Content content = CSCache.Get(CacheKey(name)) as Content;

            if (content == null)
            {
                CommonDataProvider dp = CommonDataProvider.Instance();
                content = dp.GetContent(-1, name);

                if(content == null)
                {
                    content = new Content();
                    content.Name = name;
                }
                
                if(content != null)
                {
                    CSCache.Insert(CacheKey(name), content, CSCache.MinuteFactor);

                    if(content.ID > 0)
                        CSCache.Insert(CacheKey(content.ID.ToString()), content, CSCache.MinuteFactor);
                }
            }

            return content;
        }

        

        public static void Update(Content content)
        {
                CreateUpdateDelete(content,ObjectState.Update);
        }

        public static int Add(Content content)
        {
           return CreateUpdateDelete(content,ObjectState.Create);
        }

        public static void Delete(Content content)
        {
            CreateUpdateDelete(content,ObjectState.Delete);
        }

        private static int CreateUpdateDelete(Content content, ObjectState state)
        {
           DataProviderAction action = DataProviderAction.Create;

            if(state == ObjectState.Update)
                action = DataProviderAction.Update;
            else if(state == ObjectState.Delete)
                action = DataProviderAction.Delete;

            CSEvents.PrePost(content,state,ApplicationType.ContentManagement);
            CSEvents.BeforePost(content,state,ApplicationType.ContentManagement);

            CommonDataProvider dp = CommonDataProvider.Instance();
            content.ID = dp.CreateUpdateDeleteContent(content,action);


                CSCache.Remove(CacheKey(content.ID.ToString()));
                CSCache.Remove(CacheKey(content.Name));


            CSEvents.AfterPost(content,state,ApplicationType.ContentManagement);

            return content.ID;
        }
        #endregion

        #region Utility

        /// <summary>
        /// remove illegal chars from content name
        /// </summary>
        /// <param name="sName"></param>
        /// <returns></returns>
        public static string StripBadChars(string name)
        {
            // remove bad characters
            for (int i=0; i<name.Length; i++)
                if (!Char.IsLetterOrDigit(name[i]))
                {
                    name = name.Remove(i,1);
                    i--;
                }

            return name;
        }

        private static string CacheKey(string obj)
        {
            return string.Format("Content_{0}_{1}",obj,CSContext.Current.SettingsID).ToLower();
        }
        #endregion

        public static string EditContentUrl()
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_EditContent");
        }

        public static string EditAdUrl()
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_EditAd");
        }

        public static string EditContentUrl(int id, string name)
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_formatted_EditContent", id.ToString(),name);
        }

        public static string ArticleUrl(string name)
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_articlename",name);
        }

        public static string EditArticleUrl(int id)
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_editarticle",id.ToString());
        }

        public static string NewArticleUrl()
        {
            return Globals.GetSiteUrls().UrlData.FormatUrl("contentpart_newarticle");
        }

        //

        



	}
}
